package com.djx.asynchronousorchestration.demo01;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import lombok.SneakyThrows;
import org.junit.Test;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ClassName: T02
 * @Description: TODO
 * @author: 星~
 * @date: 2021/9/17 0017 上午 11:26
 */
public class T02 {
    private static ExecutorService executor = Executors.newFixedThreadPool(10);

    private static Synccc apply(Throwable throwable) {
        if (throwable == null) {
            return null;
        }
        Synccc synccc = new Synccc();
        synccc.setThrowable(throwable);
        return synccc;
    }

    private static void accept(Synccc synccc, Throwable throwable) {
        System.out.println("方法返回结果为：" + synccc);
        System.out.println("异常为" + throwable);
    }

    private static Synccc get() {
        System.out.println("异步线程开始执行");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("执行业务流程。。。");
        System.out.println("异步线程执行结束");
//            int i = 1 / 0;
        Synccc synccc = new Synccc();
        synccc.setId(1L);
        synccc.setName("24桥");
        return synccc;
    }

    @Data
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    static class Synccc {
        private Long id;
        private String name;
        private Throwable throwable;
    }

    @SneakyThrows
    @Test
    @RequestMapping("/test03")
    public Synccc test04() {
        CompletableFuture<Synccc> exceptionally = CompletableFuture.supplyAsync(T02::get, executor).whenComplete(T02::accept).exceptionally(T02::apply);
        return exceptionally.get();
    }

    public static void main(String[] args) {
        String c = "{\"checkStatus\":\"true\",\"data\":\"{\\\"data\\\":{\\\"ret\\\":[{\\\"rect\\\":{\\\"top\\\":20,\\\"left\\\":508,\\\"width\\\":201,\\\"height\\\":21},\\\"probability\\\":{\\\"average\\\":0.999212,\\\"min\\\":0.992463,\\\"variance\\\":0.000004},\\\"word_name\\\":\\\"标题\\\",\\\"word\\\":\\\"中国建设银行网上银行电子回执\\\"},{\\\"rect\\\":{\\\"top\\\":210,\\\"left\\\":168,\\\"width\\\":56,\\\"height\\\":19},\\\"probability\\\":{\\\"average\\\":0.997765,\\\"min\\\":0.989227,\\\"variance\\\":0.000014},\\\"word_name\\\":\\\"金额（大写）\\\",\\\"word\\\":\\\"陆拾元整\\\"},{\\\"rect\\\":{\\\"top\\\":207,\\\"left\\\":751,\\\"width\\\":54,\\\"height\\\":23},\\\"probability\\\":{\\\"average\\\":0.991362,\\\"min\\\":0.968852,\\\"variance\\\":0.000101},\\\"word_name\\\":\\\"金额\\\",\\\"word\\\":\\\"60.00\\\"},{\\\"rect\\\":{\\\"top\\\":83,\\\"left\\\":278,\\\"width\\\":111,\\\"height\\\":35},\\\"probability\\\":{\\\"average\\\":0.999654,\\\"min\\\":0.999455,\\\"variance\\\":0.0},\\\"word_name\\\":\\\"付款人户名\\\",\\\"word\\\":\\\"福建省禹众水利水电工程有限公司\\\"},{\\\"rect\\\":{\\\"top\\\":170,\\\"left\\\":277,\\\"width\\\":110,\\\"height\\\":33},\\\"probability\\\":{\\\"average\\\":0.998468,\\\"min\\\":0.996361,\\\"variance\\\":0.000002},\\\"word_name\\\":\\\"付款人开户银行\\\",\\\"word\\\":\\\"有限公司福州沁园支行\\\"},{\\\"rect\\\":{\\\"top\\\":129,\\\"left\\\":277,\\\"width\\\":142,\\\"height\\\":14},\\\"probability\\\":{\\\"average\\\":0.99954,\\\"min\\\":0.998573,\\\"variance\\\":0.0},\\\"word_name\\\":\\\"付款人账号\\\",\\\"word\\\":\\\"35001616107052509488\\\"},{\\\"rect\\\":{\\\"top\\\":0,\\\"left\\\":0,\\\"width\\\":0,\\\"height\\\":0},\\\"probability\\\":{\\\"average\\\":0.0,\\\"min\\\":0.0,\\\"variance\\\":0.0},\\\"word_name\\\":\\\"摘要\\\",\\\"word\\\":\\\"\\\"},{\\\"rect\\\":{\\\"top\\\":49,\\\"left\\\":47,\\\"width\\\":98,\\\"height\\\":12},\\\"probability\\\":{\\\"average\\\":0.932503,\\\"min\\\":0.422271,\\\"variance\\\":0.021504},\\\"word_name\\\":\\\"验证码\\\",\\\"word\\\":\\\"17833887494803\\\"},{\\\"rect\\\":{\\\"top\\\":57,\\\"left\\\":463,\\\"width\\\":74,\\\"height\\\":18},\\\"probability\\\":{\\\"average\\\":0.997559,\\\"min\\\":0.978896,\\\"variance\\\":0.000035},\\\"word_name\\\":\\\"记账日期\\\",\\\"word\\\":\\\"2021-10-11\\\"},{\\\"rect\\\":{\\\"top\\\":0,\\\"left\\\":0,\\\"width\\\":0,\\\"height\\\":0},\\\"probability\\\":{\\\"average\\\":0.0,\\\"min\\\":0.0,\\\"variance\\\":0.0},\\\"word_name\\\":\\\"业务类型\\\",\\\"word\\\":\\\"\\\"},{\\\"rect\\\":{\\\"top\\\":160,\\\"left\\\":858,\\\"width\\\":108,\\\"height\\\":35},\\\"probability\\\":{\\\"average\\\":0.998529,\\\"min\\\":0.996153,\\\"variance\\\":0.000003},\\\"word_name\\\":\\\"收款人开户银行\\\",\\\"word\\\":\\\"泉州银行股份有限公司三明分行\\\"},{\\\"rect\\\":{\\\"top\\\":84,\\\"left\\\":857,\\\"width\\\":114,\\\"height\\\":34},\\\"probability\\\":{\\\"average\\\":0.997612,\\\"min\\\":0.995261,\\\"variance\\\":0.000006},\\\"word_name\\\":\\\"收款人户名\\\",\\\"word\\\":\\\"三明市国有融资担保有限公司\\\"},{\\\"rect\\\":{\\\"top\\\":35,\\\"left\\\":39,\\\"width\\\":112,\\\"height\\\":11},\\\"probability\\\":{\\\"average\\\":0.997968,\\\"min\\\":0.996008,\\\"variance\\\":0.000002},\\\"word_name\\\":\\\"收款人账号\\\",\\\"word\\\":\\\"0000018892188012\\\"},{\\\"rect\\\":{\\\"top\\\":0,\\\"left\\\":0,\\\"width\\\":0,\\\"height\\\":0},\\\"probability\\\":{\\\"average\\\":0.0,\\\"min\\\":0.0,\\\"variance\\\":0.0},\\\"word_name\\\":\\\"电子回单号码\\\",\\\"word\\\":\\\"\\\"},{\\\"rect\\\":{\\\"top\\\":246,\\\"left\\\":157,\\\"width\\\":148,\\\"height\\\":18},\\\"probability\\\":{\\\"average\\\":0.977696,\\\"min\\\":0.733615,\\\"variance\\\":0.003897},\\\"word_name\\\":\\\"用途\\\",\\\"word\\\":\\\"ES504260401100158001\\\"},{\\\"rect\\\":{\\\"top\\\":0,\\\"left\\\":0,\\\"width\\\":0,\\\"height\\\":0},\\\"probability\\\":{\\\"average\\\":0.0,\\\"min\\\":0.0,\\\"variance\\\":0.0},\\\"word_name\\\":\\\"others\\\",\\\"word\\\":[{\\\"rect\\\":{\\\"top\\\":56,\\\"left\\\":110,\\\"width\\\":83,\\\"height\\\":20},\\\"probability\\\":{\\\"average\\\":0.961108,\\\"min\\\":0.880856,\\\"variance\\\":0.001934},\\\"words\\\":\\\"币别:人民币\\\"},{\\\"rect\\\":{\\\"top\\\":57,\\\"left\\\":893,\\\"width\\\":139,\\\"height\\\":17},\\\"probability\\\":{\\\"average\\\":0.995751,\\\"min\\\":0.947587,\\\"variance\\\":0.000166},\\\"words\\\":\\\"凭证号:10384687729\\\"},{\\\"rect\\\":{\\\"top\\\":150,\\\"left\\\":279,\\\"width\\\":112,\\\"height\\\":24},\\\"probability\\\":{\\\"average\\\":0.999887,\\\"min\\\":0.999699,\\\"variance\\\":0.0},\\\"words\\\":\\\"中国建设银行股份\\\"},{\\\"rect\\\":{\\\"top\\\":280,\\\"left\\\":68,\\\"width\\\":177,\\\"height\\\":18},\\\"probability\\\":{\\\"average\\\":0.999645,\\\"min\\\":0.997822,\\\"variance\\\":0.0},\\\"words\\\":\\\"交易状态银行受理成功\\\"},{\\\"rect\\\":{\\\"top\\\":315,\\\"left\\\":76,\\\"width\\\":109,\\\"height\\\":19},\\\"probability\\\":{\\\"average\\\":0.982458,\\\"min\\\":0.918794,\\\"variance\\\":0.001014},\\\"words\\\":\\\"制单:陈文\\\"},{\\\"rect\\\":{\\\"top\\\":350,\\\"left\\\":72,\\\"width\\\":36,\\\"height\\\":19},\\\"probability\\\":{\\\"average\\\":0.998017,\\\"min\\\":0.994299,\\\"variance\\\":0.000007},\\\"words\\\":\\\"复核:\\\"},{\\\"rect\\\":{\\\"top\\\":340,\\\"left\\\":943,\\\"width\\\":23,\\\"height\\\":25},\\\"probability\\\":{\\\"average\\\":0.884631,\\\"min\\\":0.829425,\\\"variance\\\":0.003048},\\\"words\\\":\\\"国建\\\"},{\\\"rect\\\":{\\\"top\\\":385,\\\"left\\\":73,\\\"width\\\":117,\\\"height\\\":19},\\\"probability\\\":{\\\"average\\\":0.998911,\\\"min\\\":0.997289,\\\"variance\\\":0.000001},\\\"words\\\":\\\"主管:主管\\\"},{\\\"rect\\\":{\\\"top\\\":395,\\\"left\\\":936,\\\"width\\\":45,\\\"height\\\":14},\\\"probability\\\":{\\\"average\\\":0.999292,\\\"min\\\":0.997972,\\\"variance\\\":0.000001},\\\"words\\\":\\\"电子回单专用章\\\"},{\\\"rect\\\":{\\\"top\\\":419,\\\"left\\\":51,\\\"width\\\":462,\\\"height\\\":20},\\\"probability\\\":{\\\"average\\\":0.99662,\\\"min\\\":0.904293,\\\"variance\\\":0.000267},\\\"words\\\":\\\"重要提示:银行受理成功,本回执不作为收、付款方交易确认的最终依据。\\\"}]}],\\\"templateSign\\\":\\\"bank_receipt\\\",\\\"templateName\\\":\\\"银行回单\\\",\\\"scores\\\":1.0,\\\"isStructured\\\":true,\\\"logId\\\":\\\"163391840601318\\\",\\\"version\\\":1},\\\"error_code\\\":0,\\\"error_msg\\\":\\\"\\\",\\\"log_id\\\":\\\"163391840601318\\\"}\",\"delType\":\"自动审核-校验基本户信息\",\"time\":\"2021-10-11 10:13:27\"}\n";
        final Map map = JSON.parseObject(c, Map.class);
        System.out.println(map.get("data"));
    }
}
